package com.wyh.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyh.blog.aspect.BlogInfoException;
import com.wyh.blog.mapper.UserInfoMapper;
import com.wyh.blog.model.entity.UserInfo;
import com.wyh.blog.model.vo.response.UserInfoVo;
import com.wyh.blog.model.vo.response.UserVo;
import com.wyh.blog.service.intf.UserService;
import com.wyh.blog.utils.JwtUtil;
import com.wyh.blog.utils.Md5Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfoVo login(String userName, String password) {
        //定义条件构造器
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfo::getDeleteFlag,0)
                .eq(UserInfo::getUserName,userName);
//                .eq(UserInfo::getPassword,password);
        //查找用户
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        //初始化返回值
        UserInfoVo userInfoVo = new UserInfoVo();
        //用户验证失败
        if (userInfo == null){
//            userInfoVo.setToken("");
//            userInfoVo.setId(-1);
            throw new BlogInfoException("用户名或密码错误");
//            return userInfoVo;
        }
        if (!Md5Util.verify(password,userInfo.getPassword())){
            throw new BlogInfoException("用户名或密码错误");
        }
        //用户验证成功
        userInfoVo.setId(userInfo.getId());
        HashMap<String, Object> claim = new HashMap<>();
        claim.put("userName", userInfo.getUserName());
        claim.put("password",userInfo.getPassword());
        userInfoVo.setToken(JwtUtil.genToken(claim));
        return userInfoVo;
    }

    /**
     * 通过用户id获取用户信息
     * @param id
     */
    @Override
    public UserVo getUserInfo(Integer id) {
        //条件构造器定义
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfo::getDeleteFlag,0)
                .eq(UserInfo::getId, id);
        //执行sql语句
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        if (userInfo == null) return null;
        //给要返回的对象赋值
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(userInfo,userVo);
        //返回
        return userVo;
    }

    /**
     * 通过博客id获取用户信息
     * @param id
     * @return
     */
    @Override
    public UserVo getAuthorInfo(Integer id) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfo::getDeleteFlag,0)
                .eq(UserInfo::getId,id);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        if (userInfo == null) return null;
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(userInfo,userVo);
        return userVo;
    }
}
